package com.wz.project.mydo.mydojob.controller;

import com.wz.framework.aspectj.lang.annotation.Log;
import com.wz.framework.aspectj.lang.enums.BusinessType;
import com.wz.framework.web.controller.BaseController;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.framework.web.page.TableDataInfo;
import com.wz.project.mydo.domain.MydoJob;
import com.wz.project.mydo.domain.MydoJobFinish;

import com.wz.project.mydo.mydojob.service.IJobFinishJService;
import com.wz.project.mydo.mydojob.service.IMydoJobService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作业 信息操作处理
 * 
 * @author sys
 * @date 2021-10-28
 */
@Controller
@RequestMapping("/mydo/mydojob")
public class InMydoJobController extends BaseController
{
    private String prefix = "mydo/mydojob";
	
	@Autowired
	private IMydoJobService jobService;

	@Autowired
	private IJobFinishJService finishService;
	
//	@RequiresPermissions("mydo:mydojob:view")
	@GetMapping()
	public String job()
	{
	    return prefix + "/inmydojob";
	}
	
	/**
	 * 查询作业列表
	 */
//	@RequiresPermissions("mydo:mydojob:list")
	@RequestMapping("/list")
	@ResponseBody
	public TableDataInfo list(MydoJob mydoJob, MydoJobFinish jobFinish,String inOut)
	{
		mydoJob.setDevId(inOut);
		jobFinish.setDevId(inOut);
		if ("4".equals(mydoJob.getStatus()) || "5".equals(mydoJob.getStatus())){
			startPage();
			List<MydoJobFinish> list = finishService.selectJobFinishList(jobFinish);
			return getDataTable(list);
		}else{
			startPage();
			List<MydoJob> list = jobService.selectJobList(mydoJob);
			return getDataTable(list);
		}

	}
	
	/**
	 * 新增作业
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存作业
	 */
	@RequiresPermissions("mydo:mydojob:add")
	@Log(title = "作业", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(MydoJob MydoJob)
	{		
		return toAjax(jobService.insertJob(MydoJob));
	}

	/**
	 * 修改作业
	 */
	@GetMapping("/edit/{jobId}")
	public String edit(@PathVariable("jobId") String jobId, ModelMap mmap)
	{
		MydoJob MydoJob = jobService.selectJobById(jobId);
		mmap.put("job", MydoJob);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存作业
	 */
	@RequiresPermissions("mydo:mydojob:edit")
	@Log(title = "作业", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(MydoJob MydoJob)
	{		
		return toAjax(jobService.updateJob(MydoJob));
	}
	
	/**
	 * 删除作业
	 */
	@RequiresPermissions("mydo:mydojob:remove")
	@Log(title = "作业", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(jobService.deleteJobByIds(ids));
	}

	/**
	 * 手动完成
	 * @param ids
	 * @return
	 */
	@RequiresPermissions("mydo:mydojob:handFinish")
	@RequestMapping("/handFinish")
	@ResponseBody
	public AjaxResult handFinish(String ids) {
		int i = jobService.handFinish(ids);
		if (i == 1) {
			return AjaxResult.success("手动完成成功！");
		} else if (i == 0) {
            return AjaxResult.error("只允许手动完成正执行的作业！");
		} else {
			return AjaxResult.error("未知错误，请联系管理员！");
		}

	}

	/**
	 * 手动作废
	 * @param ids
	 * @return
	 */
	@RequiresPermissions("mydo:mydojob:handInvalid")
	@RequestMapping("/handInvalid")
	@ResponseBody
	public AjaxResult handInvalid(String ids){
		int i = jobService.handInvalid(ids);
		if (i == 10){
			return AjaxResult.success("手动作废成功！");
		}else if (i == 0){
			return AjaxResult.error("只允许手动作废未执行和待执行的作业！");
		}else{
			return AjaxResult.error("未知错误，请联系管理员！");
		}
	}

	/**
	 * 更改作业状态
	 * @param ids
	 * @return
	 */
	@RequiresPermissions("mydo:mydojob:changeJobStatus")
	@RequestMapping("/changeJobStatus")
	@ResponseBody
	public AjaxResult changeJobStatus(String ids){
		String id[] = ids.split(",");
		int i = jobService.changeJobStatus(id);
		if (i == 1){
			return AjaxResult.success("更改状态成功");
		}else{
			return AjaxResult.error("只允许更改状态为正执行的作业");
		}
	}
	/**
	 * 更改优先级
	 * @param ids
	 * @return
	 */
//	@RequiresPermissions("mydo:mydojob:changeJobStatus")
	@RequestMapping("/changeJobpriority")
	@ResponseBody
	public AjaxResult changeJobpriority(String ids,int pro ){
		String id[] = ids.split(",");
		int i = jobService.changeJobpriority(id,pro);
		if (i == 1){
			return AjaxResult.success("更改状态成功");
		}else{
			return AjaxResult.error("只允许修改未执行的作业优先级");
		}
	}
}
